﻿
var JSON = function () {
    var m = {
        '\b': '\\b',
        '\t': '\\t',
        '\n': '\\n',
        '\f': '\\f',
        '\r': '\\r',
        '"': '\\"',
        '\\': '\\\\'
    },
        s = {
            'boolean': function (x) {
                return String(x);
            },
            number: function (x) {
                return isFinite(x) ? String(x) : 'null';
            },
            string: function (x) {
                if (/["\\\x00-\x1f]/.test(x)) {
                    x = x.replace(/([\x00-\x1f\\"])/g, function (a, b) {
                        var c = m[b];
                        if (c) {
                            return c;
                        }
                        c = b.charCodeAt();
                        return '\\u00' +
                            Math.floor(c / 16).toString(16) +
                            (c % 16).toString(16);
                    });
                }
                return '"' + x + '"';
            },
            object: function (x) {
                if (x) {
                    var a = [], b, f, i, l, v;
                    if (x instanceof Array) {
                        a[0] = '[';
                        l = x.length;
                        for (i = 0; i < l; i += 1) {
                            v = x[i];
                            f = s[typeof v];
                            if (f) {
                                v = f(v);
                                if (typeof v == 'string') {
                                    if (b) {
                                        a[a.length] = ',';
                                    }
                                    a[a.length] = v;
                                    b = true;
                                }
                            }
                        }
                        a[a.length] = ']';
                    } else if (x instanceof Object) {
                        a[0] = '{';
                        for (i in x) {
                            v = x[i];
                            f = s[typeof v];
                            if (f) {
                                v = f(v);
                                if (typeof v == 'string') {
                                    if (b) {
                                        a[a.length] = ',';
                                    }
                                    a.push(s.string(i), ':', v);
                                    b = true;
                                }
                            }
                        }
                        a[a.length] = '}';
                    } else {
                        return;
                    }
                    return a.join('');
                }
                return 'null';
            }
        };
    return {
        copyright: '(c)2005 JSON.org',
        license: 'http://www.JSON.org/license.html',

        stringify: function (v) {
            var f = s[typeof v];
            if (f) {
                v = f(v);
                if (typeof v == 'string') {
                    return v;
                }
            }
            return null;
        },

        parse: function (text) {
            try {
                return !(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
                        text.replace(/"(\\.|[^"\\])*"/g, ''))) &&
                    eval('(' + text + ')');
            } catch (e) {
                return false;
            }
        }
    };
} ();

(function ($) {
    $.toJSON = function (o) {
        if (typeof (JSON) == 'object' && JSON.stringify)
            return JSON.stringify(o); var type = typeof (o); if (o === null)
            return "null"; if (type == "undefined")
            return undefined; if (type == "number" || type == "boolean")
            return o + ""; if (type == "string")
            return $.quoteString(o); if (type == 'object') {
            if (typeof o.toJSON == "function")
                return $.toJSON(o.toJSON()); if (o.constructor === Date) {
                var month = o.getUTCMonth() + 1; if (month < 10) month = '0' + month; var day = o.getUTCDate(); if (day < 10) day = '0' + day; var year = o.getUTCFullYear(); var hours = o.getUTCHours(); if (hours < 10) hours = '0' + hours; var minutes = o.getUTCMinutes(); if (minutes < 10) minutes = '0' + minutes; var seconds = o.getUTCSeconds(); if (seconds < 10) seconds = '0' + seconds; var milli = o.getUTCMilliseconds(); if (milli < 100) milli = '0' + milli; if (milli < 10) milli = '0' + milli; return '"' + year + '-' + month + '-' + day + 'T' +
hours + ':' + minutes + ':' + seconds + '.' + milli + 'Z"';
            }
            if (o.constructor === Array) {
                var ret = []; for (var i = 0; i < o.length; i++)
                    ret.push($.toJSON(o[i]) || "null"); return "[" + ret.join(",") + "]";
            }
            var pairs = []; for (var k in o) {
                var name; var type = typeof k; if (type == "number")
                    name = '"' + k + '"'; else if (type == "string")
                    name = $.quoteString(k); else
                    continue; if (typeof o[k] == "function")
                    continue; var val = $.toJSON(o[k]); pairs.push(name + ":" + val);
            }
            return "{" + pairs.join(", ") + "}";
        }
    };
    $.evalJSON = function (src) {
        if (typeof (JSON) == 'object' && JSON.parse)
            return JSON.parse(src);
        return eval("(" + src + ")");
    };
    $.secureEvalJSON = function (src) {
        if (typeof (JSON) == 'object' && JSON.parse)
            return JSON.parse(src); var filtered = src; filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@'); filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']'); filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, ''); if (/^[\],:{}\s]*$/.test(filtered))
            return eval("(" + src + ")"); else
            throw new SyntaxError("Error parsing JSON, source is not valid.");
    };
    $.quoteString = function (string) {
        if (string.match(_escapeable)) {
            return '"' + string.replace(_escapeable, function (a)
            { var c = _meta[a]; if (typeof c === 'string') return c; c = a.charCodeAt(); return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16); }) + '"';
        }
        return '"' + string + '"';
    };
    var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g; var _meta = { '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"': '\\"', '\\': '\\\\' };
})(jQuery);

/**
* Get the value of a cookie with the given name.
*
* @example $.cookie('the_cookie');
* @desc Get the value of a cookie.
*
* @param String name The name of the cookie.
* @return The value of the cookie.
* @type String
*
* @name $.cookie
* @cat Plugins/Cookie
* @author Klaus Hartl/klaus.hartl@stilbuero.de
*/
jQuery.cookie = function (name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

$.log = function (l) {
    console.log(l);
}

function ShowMiniPop(e) {
    var holder = $('#minipop-holder');
    var offset = $(this).offset();
    var _this = $(this);
    var title, top, left;
    if (typeof (e.data.title) == 'function') {
        e.data.title(
            function (t, scc) {
                if (scc == false) {
                    $('#minipop-hint-close').show();
                    $('#minipop-hint-tip').html(t);
                    $('#minipop-action').hide();
                    $('#minipop-hint').show();
                }
                else {
                    $('#minipop-hint').hide();
                    $('#minipop-action').show();
                    $('#minipop-tip').html(t);
                }
                holder.fadeIn();
                top = offset.top - holder.outerHeight() - 5;
                if (top < 0) {
                    top = offset.top + _this.outerHeight() + 5;
                }

                left = offset.left - holder.outerWidth() / 2;
                holder.css({
                    'top': top + 'px',
                    'left': left + 12 + 'px'
                });

                if (scc == false) {
                    holder.fadeOut(2000);
                }
            }, _this
        );
    }
    else {
        $('#minipop-hint').hide();
        $('#minipop-action').show();
        holder.fadeIn();
        title = e.data.title
        $('#minipop-tip').html(title);
        //距离上面的空白
        top = offset.top - holder.outerHeight() - 5;
        if (top < 0) {
            top = offset.top + $(this).outerHeight() + 5;
        }

        left = offset.left - holder.outerWidth() / 2;
        holder.css({
            'top': top + 'px',
            'left': left + 12 + 'px'
        });
    }

    $('#minipop-submit').bind('click', e.data.argus, e.data.fun);
    $('#minipop-cancel').bind('click', HideMiniPop);
}

function ShowMiniHint(e) {
    var error = '';

    if (typeof (e.data.check) == 'function') {
        error = e.data.check();
    }

    var holder = $('#minipop-holder');
    $('#minipop-action').hide();
    $('#minipop-hint').show();
    holder.fadeIn();
    if (error != '') {
        $('#minipop-hint-tip').html(error);
    }
    else {
        $('#minipop-hint-tip').html('<img src="/Common/images/ajax-loader.gif" style="vertical-align:middle" />&nbsp;&nbsp;正在提交...');
    }
    var offset = $(this).offset();

    //距离上面的空白
    var top = offset.top - holder.outerHeight() - 5;
    if (top < 0) {
        top = offset.top + $(this).outerHeight() + 5;
    }

    var left = offset.left + -holder.outerWidth() / 2;
    holder.css({
        'top': top + 'px',
        'left': left + 12 + 'px'
    });

    if (error != '') {
        holder.fadeOut(2000);
        return;
    }
    else {
        e.data.fun(e.data.argus);
    }
}

function AddMiniHint(content) {
    $('#minipop-hint-tip').html(content);
    $('#minipop-action').hide();
    $('#minipop-hint').show();
    HideMiniPop(2000);
}

function AddErrMiniHint(content) {
    $('#minipop-holder').show();
    $('#minipop-hint-close').show();
    $('#minipop-hint-tip').html(content);
    $('#minipop-action').hide();
    $('#minipop-hint').show();
}

function AddSucceedMiniHint(content) {
    $('#minipop-holder').show();
    $('#minipop-hint-close').show();
    $('#minipop-hint-tip').html(content);
    $('#minipop-action').hide();
    $('#minipop-hint').show();
}

function HideMiniPop(timeout) {
    $('#minipop-submit').unbind();
    $('#minipop-cancel').unbind();
    if (timeout) {
        $('#minipop-holder').fadeOut(timeout);
    }
    else {
        $('#minipop-holder').hide();
    }
}

function Num2ABC(i) {
    return [" ", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"][i];
}

//将字符串的前后空格去掉，再把中间的多个空格替换成一个空格
function stripSpace(s) {
    return s.replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s+/, ' ')
}

function log(l){
    console.log(l);
}

function StringBuffer() {
    this.strings = [];
}
StringBuffer.prototype.Append = function (str) {
    this.strings.push(str);
};
StringBuffer.prototype.toString = function () {
    return this.strings.join("");
};

//处理解释
function BlodPh(x) {
    return x.replace(/\[def\]((.|\n|\r)*?)\[\/def\]/gim, function (a, b) {
        b = ReplacePh(b);
        return "<div>" + b + "</div>";
    });
}

function ReplacePh(b) {
    return b.replace(/(abbr\.|prep\.|pron\.|n\.|v\.|conj\.|vi\.|vt\.|aux\.v\.|modal v\.|art\.|num\.|int\.|\[U\]|\[C\]|adj\.|a\.|adv\.|ad\.|\[U,C\]|im\.)/gim, '<span class="def">$1</span>');
}

